<!DOCTYPE html>
<html lang="zh-CN">
<head>
  <meta charset="UTF-8">
<meta name="viewport" content="width=device-width">
<meta name="theme-color" content="#222">
<meta name="generator" content="Hexo 5.4.2">


  <link rel="apple-touch-icon" sizes="180x180" href="/page/images/apple-touch-icon-next.png">
  <link rel="icon" type="image/png" sizes="32x32" href="/page/images/favicon-32x32-next.png">
  <link rel="icon" type="image/png" sizes="16x16" href="/page/images/favicon-16x16-next.png">
  <link rel="mask-icon" href="/page/images/logo.svg" color="#222">

<link rel="stylesheet" href="/page/css/main.css">



<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/@fortawesome/fontawesome-free@5.15.4/css/all.min.css" integrity="sha256-mUZM63G8m73Mcidfrv5E+Y61y7a12O5mW4ezU3bxqW4=" crossorigin="anonymous">
  <link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/animate.css@3.1.1/animate.min.css" integrity="sha256-PR7ttpcvz8qrF57fur/yAx1qXMFJeJFiA6pSzWi0OIE=" crossorigin="anonymous">

<script class="next-config" data-name="main" type="application/json">{"hostname":"assd12138.gitee.io","root":"/page/","images":"/page/images","scheme":"Gemini","darkmode":false,"version":"8.8.0","exturl":false,"sidebar":{"position":"left","display":"post","padding":18,"offset":12},"copycode":false,"bookmark":{"enable":false,"color":"#222","save":"auto"},"mediumzoom":false,"lazyload":false,"pangu":false,"comments":{"style":"tabs","active":null,"storage":true,"lazyload":false,"nav":null},"stickytabs":false,"motion":{"enable":true,"async":false,"transition":{"post_block":"fadeIn","post_header":"fadeInDown","post_body":"fadeInDown","coll_header":"fadeInLeft","sidebar":"fadeInUp"}},"prism":false,"i18n":{"placeholder":"搜索...","empty":"没有找到任何搜索结果：${query}","hits_time":"找到 ${hits} 个搜索结果（用时 ${time} 毫秒）","hits":"找到 ${hits} 个搜索结果"},"path":"/page/search.json","localsearch":{"enable":true,"trigger":"auto","top_n_per_article":1,"unescape":false,"preload":false}}</script><script src="/page/js/config.js"></script>
<meta name="description" content="CSS3的transform有很丰富的变换类型，可以做出各种效果">
<meta property="og:type" content="article">
<meta property="og:title" content="学习transform">
<meta property="og:url" content="https://assd12138.gitee.io/page/2020/09/26/%E5%AD%A6%E4%B9%A0transform/index.html">
<meta property="og:site_name" content="爱爱爱呵风他">
<meta property="og:description" content="CSS3的transform有很丰富的变换类型，可以做出各种效果">
<meta property="og:locale" content="zh_CN">
<meta property="og:image" content="https://gitee.com/assd12138/cdnpics/raw/master/img/QQ%E5%9B%BE%E7%89%8720201025144808.png">
<meta property="article:published_time" content="2020-09-26T01:23:21.000Z">
<meta property="article:modified_time" content="2022-05-09T03:18:13.063Z">
<meta property="article:author" content="爱爱爱呵风他">
<meta property="article:tag" content="CSS">
<meta name="twitter:card" content="summary">
<meta name="twitter:image" content="https://gitee.com/assd12138/cdnpics/raw/master/img/QQ%E5%9B%BE%E7%89%8720201025144808.png">


<link rel="canonical" href="https://assd12138.gitee.io/page/2020/09/26/%E5%AD%A6%E4%B9%A0transform/">



<script class="next-config" data-name="page" type="application/json">{"sidebar":"","isHome":false,"isPost":true,"lang":"zh-CN","comments":true,"permalink":"https://assd12138.gitee.io/page/2020/09/26/%E5%AD%A6%E4%B9%A0transform/","path":"2020/09/26/学习transform/","title":"学习transform"}</script>

<script class="next-config" data-name="calendar" type="application/json">""</script>
<title>学习transform | 爱爱爱呵风他</title>
  




  <noscript>
    <link rel="stylesheet" href="/page/css/noscript.css">
  </noscript>
</head>

<body itemscope itemtype="http://schema.org/WebPage" class="use-motion">
  <div class="headband"></div>

  <main class="main">
    <header class="header" itemscope itemtype="http://schema.org/WPHeader">
      <div class="header-inner"><div class="site-brand-container">
  <div class="site-nav-toggle">
    <div class="toggle" aria-label="切换导航栏" role="button">
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
        <span class="toggle-line"></span>
    </div>
  </div>

  <div class="site-meta">

    <a href="/page/" class="brand" rel="start">
      <i class="logo-line"></i>
      <h1 class="site-title">爱爱爱呵风他</h1>
      <i class="logo-line"></i>
    </a>
  </div>

  <div class="site-nav-right">
    <div class="toggle popup-trigger">
        <i class="fa fa-search fa-fw fa-lg"></i>
    </div>
  </div>
</div>



<nav class="site-nav">
  <ul class="main-menu menu">
        <li class="menu-item menu-item-home"><a href="/page/" rel="section"><i class="fa fa-home fa-fw"></i>首页</a></li>
        <li class="menu-item menu-item-tags"><a href="/page/tags/" rel="section"><i class="fa fa-tags fa-fw"></i>标签</a></li>
        <li class="menu-item menu-item-archives"><a href="/page/archives/" rel="section"><i class="fa fa-archive fa-fw"></i>归档</a></li>
      <li class="menu-item menu-item-search">
        <a role="button" class="popup-trigger"><i class="fa fa-search fa-fw"></i>搜索
        </a>
      </li>
  </ul>
</nav>



  <div class="search-pop-overlay">
    <div class="popup search-popup"><div class="search-header">
  <span class="search-icon">
    <i class="fa fa-search"></i>
  </span>
  <div class="search-input-container">
    <input autocomplete="off" autocapitalize="off" maxlength="80"
           placeholder="搜索..." spellcheck="false"
           type="search" class="search-input">
  </div>
  <span class="popup-btn-close" role="button">
    <i class="fa fa-times-circle"></i>
  </span>
</div>
<div class="search-result-container no-result">
  <div class="search-result-icon">
    <i class="fa fa-spinner fa-pulse fa-5x"></i>
  </div>
</div>

    </div>
  </div>

</div>
        
  
  <div class="toggle sidebar-toggle" role="button">
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
    <span class="toggle-line"></span>
  </div>

  <aside class="sidebar">

    <div class="sidebar-inner sidebar-nav-active sidebar-toc-active">
      <ul class="sidebar-nav">
        <li class="sidebar-nav-toc">
          文章目录
        </li>
        <li class="sidebar-nav-overview">
          站点概览
        </li>
      </ul>

      <div class="sidebar-panel-container">
        <!--noindex-->
        <div class="post-toc-wrap sidebar-panel">
            <div class="post-toc animated"><ol class="nav"><li class="nav-item nav-level-2"><a class="nav-link" href="#%E8%BD%AC%E6%8D%A2%E7%9A%84%E6%9F%A5%E7%9C%8B%E6%96%B9%E6%B3%95"><span class="nav-number">1.</span> <span class="nav-text">转换的查看方法</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#translate"><span class="nav-number">2.</span> <span class="nav-text">translate</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#scale"><span class="nav-number">3.</span> <span class="nav-text">scale</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#rotate"><span class="nav-number">4.</span> <span class="nav-text">rotate</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#skew"><span class="nav-number">5.</span> <span class="nav-text">skew</span></a></li><li class="nav-item nav-level-2"><a class="nav-link" href="#transform-origin"><span class="nav-number">6.</span> <span class="nav-text">transform-origin</span></a></li></ol></div>
        </div>
        <!--/noindex-->

        <div class="site-overview-wrap sidebar-panel">
          <div class="site-author site-overview-item animated" itemprop="author" itemscope itemtype="http://schema.org/Person">
    <img class="site-author-image" itemprop="image" alt="爱爱爱呵风他"
      src="https://s1.ax1x.com/2020/08/26/dWnF58.jpg">
  <p class="site-author-name" itemprop="name">爱爱爱呵风他</p>
  <div class="site-description" itemprop="description"></div>
</div>
<div class="site-state-wrap site-overview-item animated">
  <nav class="site-state">
      <div class="site-state-item site-state-posts">
        <a href="/page/archives/">
          <span class="site-state-item-count">24</span>
          <span class="site-state-item-name">日志</span>
        </a>
      </div>
      <div class="site-state-item site-state-tags">
          <a href="/page/tags/">
        <span class="site-state-item-count">11</span>
        <span class="site-state-item-name">标签</span></a>
      </div>
  </nav>
</div>
  <div class="cc-license site-overview-item animated" itemprop="license">
    <a href="https://creativecommons.org/licenses/by-nc-sa/4.0/zh-cn" class="cc-opacity" rel="noopener" target="_blank"><img src="https://cdn.jsdelivr.net/npm/@creativecommons/vocabulary@2020.11.3/assets/license_badges/small/by_nc_sa.svg" alt="Creative Commons"></a>
  </div>



        </div>
      </div>
    </div>
  </aside>
  <div class="sidebar-dimmer"></div>


    </header>

    
  <div class="back-to-top" role="button" aria-label="返回顶部">
    <i class="fa fa-arrow-up"></i>
    <span>0%</span>
  </div>

<noscript>
  <div class="noscript-warning">Theme NexT works best with JavaScript enabled</div>
</noscript>


    <div class="main-inner post posts-expand">


  


<div class="post-block">
  
  

  <article itemscope itemtype="http://schema.org/Article" class="post-content" lang="zh-CN">
    <link itemprop="mainEntityOfPage" href="https://assd12138.gitee.io/page/2020/09/26/%E5%AD%A6%E4%B9%A0transform/">

    <span hidden itemprop="author" itemscope itemtype="http://schema.org/Person">
      <meta itemprop="image" content="https://s1.ax1x.com/2020/08/26/dWnF58.jpg">
      <meta itemprop="name" content="爱爱爱呵风他">
      <meta itemprop="description" content="">
    </span>

    <span hidden itemprop="publisher" itemscope itemtype="http://schema.org/Organization">
      <meta itemprop="name" content="爱爱爱呵风他">
    </span>
      <header class="post-header">
        <h1 class="post-title" itemprop="name headline">
          学习transform
        </h1>

        <div class="post-meta-container">
          <div class="post-meta">
    <span class="post-meta-item">
      <span class="post-meta-item-icon">
        <i class="far fa-calendar"></i>
      </span>
      <span class="post-meta-item-text">发表于</span>

      <time title="创建时间：2020-09-26 09:23:21" itemprop="dateCreated datePublished" datetime="2020-09-26T09:23:21+08:00">2020-09-26</time>
    </span>
      <span class="post-meta-item">
        <span class="post-meta-item-icon">
          <i class="far fa-calendar-check"></i>
        </span>
        <span class="post-meta-item-text">更新于</span>
        <time title="修改时间：2022-05-09 11:18:13" itemprop="dateModified" datetime="2022-05-09T11:18:13+08:00">2022-05-09</time>
      </span>

  
</div>

        </div>
      </header>

    
    
    
    <div class="post-body" itemprop="articleBody">
        <p>CSS3的transform有很丰富的变换类型，可以做出各种效果</p>
<span id="more"></span>

<h2 id="转换的查看方法"><a href="#转换的查看方法" class="headerlink" title="转换的查看方法"></a>转换的查看方法</h2><p>使用火狐浏览器的控制台可以查看转换前和转换后的状态及对应点的连线，选中转换后的元素，鼠标移动到css中的transform的值上，即可显示如下的转换过程。<br><img src="https://gitee.com/assd12138/cdnpics/raw/master/img/QQ%E5%9B%BE%E7%89%8720201025144808.png" alt="firefox查看转换的示例图片"></p>
<h2 id="translate"><a href="#translate" class="headerlink" title="translate"></a>translate</h2><p><code>translate(tx,ty)</code> 这个CSS 函数用于移动元素在平面上的位置。接收两个值，分别表示元素在笛卡尔坐标系上进行横向和纵向移动的值，第二个参数不写默认为0。<br><code>translateX(tx)</code>和<code>translateY(ty)</code>都默认另一个方向上的值为0，可以用<code>translate(tx,ty)</code>代替</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.case1</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">  <span class="attribute">transform</span>: <span class="built_in">translate</span>(<span class="number">12px</span>, <span class="number">50%</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>

<button class="case1-btn">转变</button>
<div class="case1"></div>
<script>
  const case1Div = document.querySelector('.case1');
  document.querySelector('.case1-btn').addEventListener('click', () => {
    case1Div.style.transform = case1Div.style.transform === 'none' ? 'translate(12px, 50%)' : 'none';
  })
</script>
<style>
  .case1 {
    height: 100px;
    width: 100px;
    border: 1px solid red;
    transform: translate(12px, 50%);
  }
</style>


<hr>
<h2 id="scale"><a href="#scale" class="headerlink" title="scale"></a>scale</h2><p><code>scale(sx,sy)</code>用于定义缩放，缩放的方法从盒子的<code>transform-origin</code>向两侧/上下缩放，sx是横向缩放的比例，sy是纵向缩放的比例，如果不填sy，那么sy的默认值为sx即等比例缩放。和<code>translate</code>类似的，<code>scale</code>也有<code>scaleX</code>和<code>scaleY</code>,但是使用这两个另一个方向上的默认值为1，表示1:1不缩放。</p>
<blockquote>
<p>在Firefox浏览器下，缩放比例为浮点数时，可能会导致边框渲染不正常无法显示(<a target="_blank" rel="noopener" href="https://bugzilla.mozilla.org/show_bug.cgi?id=1476379">bug1476379</a>),下面例子中的第二个盒子在firefox中可能无法渲染出部分边框</p>
</blockquote>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.case2</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">  <span class="attribute">transform</span>: <span class="built_in">scale</span>(<span class="number">1.2</span>,<span class="number">0.6</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>

  <button style="position: relative; z-index: 2;"" class="case2-btn">转变</button>
  <div class="case2"></div>
  <div class="case2" style="scale: 0.5 0.5;"></div>
  <script>
  const case2Div = document.querySelector('.case2');
  const case2DivDefault = case2Div.style.transform
  document.querySelector('.case2-btn').addEventListener('click', () => {
    case2Div.style.transform = case2Div.style.transform === 'none' ? case2DivDefault : 'none';
  })
  </script>
  <style>
    .case2 {
      height: 100px;
      width: 100px;
      border: 1px solid red;
      transform: scale(1.2,0.6);
    }
  </style>


<hr>
<h2 id="rotate"><a href="#rotate" class="headerlink" title="rotate"></a>rotate</h2><p><code>rotate(a)</code>以<code>transform-origin</code>为不动点旋转一个元素（可以把整个元素想象成一张非常大的纸上的一块区域，然后按住不动点，转这张纸一定角度，这块区域也会随之旋转）。正值为顺时针，负值为逆时针。</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="http://caibaojian.com/css3/values/angle/index.htm">CSS角度值的单位</a></p>
</blockquote>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br></pre></td><td class="code"><pre><span class="line"><span class="selector-class">.case3</span> &#123;</span><br><span class="line">  <span class="attribute">height</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">width</span>: <span class="number">100px</span>;</span><br><span class="line">  <span class="attribute">border</span>: <span class="number">1px</span> solid red;</span><br><span class="line">  <span class="attribute">transform</span>: <span class="built_in">rotate</span>(<span class="number">0.3turn</span>);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure>

<hr>

  <button style="position: relative; z-index: 2;"" class="case3-btn">转变</button>
  <div class="case3"></div>
<script>
  const case3Div = document.querySelector('.case3');
  const case3DivDefault = case3Div.style.transform
  document.querySelector('.case3-btn').addEventListener('click', () => {
    case3Div.style.transform = case3Div.style.transform === 'none' ? case3DivDefault : 'none';
  })
</script>
<style>
  .case3 {
    height: 100px;
    width: 100px;
    border: 1px solid red;
    transform: rotate(0.3turn);
  }
</style>


<hr>
<h2 id="skew"><a href="#skew" class="headerlink" title="skew"></a>skew</h2><p>倾斜，这个变换比较复杂，可以查看<a target="_blank" rel="noopener" href="https://juejin.im/post/6854573212743761934">这篇文章</a></p>
<h2 id="transform-origin"><a href="#transform-origin" class="headerlink" title="transform-origin"></a>transform-origin</h2><p><code>transform-origin</code>默认如下</p>
<figure class="highlight css"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="attribute">transform-origin</span>:<span class="number">50%</span> <span class="number">50%</span> <span class="number">0</span></span><br></pre></td></tr></table></figure>

<p>也就是盒模型的几何中心</p>
<blockquote>
<p><a target="_blank" rel="noopener" href="https://developer.mozilla.org/zh-CN/docs/Web/CSS/transform-function">transform的图形转换原理</a></p>
</blockquote>

    </div>

    
    
    

    <footer class="post-footer">
          <div class="post-tags">
              <a href="/page/tags/CSS/" rel="tag"># CSS</a>
          </div>

        

          <div class="post-nav">
            <div class="post-nav-item">
                <a href="/page/2020/09/26/Vue%E7%9A%84%E7%88%B6%E5%AD%90%E7%94%9F%E5%91%BD%E5%91%A8%E6%9C%9F/" rel="prev" title="Vue的父子生命周期">
                  <i class="fa fa-chevron-left"></i> Vue的父子生命周期
                </a>
            </div>
            <div class="post-nav-item">
                <a href="/page/2020/09/30/linux%E7%AC%94%E8%AE%B0/" rel="next" title="linux笔记">
                  linux笔记 <i class="fa fa-chevron-right"></i>
                </a>
            </div>
          </div>
    </footer>
  </article>
</div>






    <div class="comments utterances-container"></div>
</div>
  </main>

  <footer class="footer">
    <div class="footer-inner">


<div class="copyright">
  &copy; 
  <span itemprop="copyrightYear">2023</span>
  <span class="with-love">
    <i class="fa fa-heart"></i>
  </span>
  <span class="author" itemprop="copyrightHolder">爱爱爱呵风他</span>
</div>
  <div class="powered-by">由 <a href="https://hexo.io/" rel="noopener" target="_blank">Hexo</a> & <a href="https://theme-next.js.org/" rel="noopener" target="_blank">NexT.Gemini</a> 强力驱动
  </div>

    </div>
  </footer>

  
  <script size="300" alpha="0.6" zIndex="-1" src="https://cdn.jsdelivr.net/npm/ribbon.js@1.0.2/dist/ribbon.min.js"></script>
  <script src="https://cdn.jsdelivr.net/npm/animejs@3.2.1/lib/anime.min.js" integrity="sha256-XL2inqUJaslATFnHdJOi9GfQ60on8Wx1C2H8DYiN1xY=" crossorigin="anonymous"></script>
<script src="/page/js/comments.js"></script><script src="/page/js/utils.js"></script><script src="/page/js/motion.js"></script><script src="/page/js/next-boot.js"></script>

  
<script src="https://cdn.jsdelivr.net/npm/hexo-generator-searchdb@1.4.0/dist/search.js" integrity="sha256-vXZMYLEqsROAXkEw93GGIvaB2ab+QW6w3+1ahD9nXXA=" crossorigin="anonymous"></script>
<script src="/page/js/third-party/search/local-search.js"></script>





  




<script class="next-config" data-name="utterances" type="application/json">{"enable":true,"repo":"assd12138/utterancesComment","issue_term":"pathname","theme":"preferred-color-scheme"}</script>
<script src="/page/js/third-party/comments/utterances.js"></script>

</body>
</html>
